﻿using DnD.Common;
using DnD.Model;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DnD.ViewModels
{
    class UserViewModel : INotifyPropertyChanged
    {

        private PhoneApplicationPage _view;

        public static String userName;
        public static String sessionID;

        public String UserName
        {
            get { return userName; }
            set
            {
                userName = value;
                OnPropertyChanged();

            }
        }

        private String _password;
        public String Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged();

            }
        }

        private String _userDesc;
        public String userDescription
        {
            get { return _userDesc; }
            set
            {
                _userDesc = value;
                OnPropertyChanged();

            }
        }


        public String SessionID
        {
            get { return sessionID; }
            set
            {
                if (value != "ERROR")
                {
                    sessionID = value;
                    OnPropertyChanged();
                    MessageBox.Show("Succesfully logged in!");
                    App.appStateMap["userName"] = userName;
                    App.appStateMap["sessionID"] = sessionID;

                    _view.NavigationService.GoBack();
                }
                else
                {
                    MessageBox.Show("Problem during login!");
                }

            }
        }

        public void handleMenuRequest(String menuItemName)
        {
            if (menuItemName == "login")
            {
                _view.NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
            }
            else if (menuItemName == "map")
            {
                _view.NavigationService.Navigate(new Uri("/MapPage.xaml", UriKind.Relative));
            }
 
        }


        public DelegateCommand LoginCommand { get; set; }

        public UserViewModel(PhoneApplicationPage view)
        {
            _view = view;

            LoginCommand = new DelegateCommand(loginUser, name => UserName != "" && Password != "");

            //if (App.appStateMap.ContainsKey("userName") && App.appStateMap.ContainsKey("sessionID") && App.appStateMap["userName"] != null && App.appStateMap["sessionID"] != null)
            //{
            //    UserName = (string)App.appStateMap["userName"];
            //    sessionID = (string)App.appStateMap["sessionID"];

            //}
        }

        public void loginUser(object name)
        {
            DnDRestService restService = new DnDRestService();
            restService.loginUser(UserName,Password,session => SessionID =session);

        }

        public void refreshUserData()
        {
            if (sessionID != null && sessionID != "" || SessionID == "ERROR")
            {
                UserName = userName;
            }
            else
            {
                _view.NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
            }

        }

        public void cancelLogin()
        {
            if (SessionID == null || SessionID == "" || SessionID == "ERROR")
            {
                
                _view.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
        }

        public static bool checkIfUserLoggedIn()
        {
            if (userName != null && userName != "" && sessionID != null && sessionID != "" && sessionID != "ERROR")
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        public string BackGroundImageSource
        {
            get
            {
                if ((Visibility)App.Current.Resources["PhoneDarkThemeVisibility"]
                  == Visibility.Visible)
                {
                    return "/Assets/Pictures/fire3.jpg";

                }
                else
                {
                    return "/Assets/Pictures/fire3_negative.jpg";

                }
            }
            private set { }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
